<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use app\common\model\Website;
use Hisune\EchartsPHP\ECharts;
use think\Db;

class Index extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function index()
    {
        return $this->view->fetch();
    }

    public function chartpic()
    {

        $param = request()->param();
        $where = [];
        $vioWhere = [];
        if (
            !empty($param['date_start'])
            &&\think\Validate::dateFormat($param['date_start'],'Y-m-d')
            &&!empty($param['date_end'])
            &&\think\Validate::dateFormat($param['date_end'],'Y-m-d') 
            && strtotime($param['date_end']) >= strtotime($param['date_start'])
        ) {
            $where['created_at']=[['egt',strtotime($param['date_start']) ],['elt',strtotime($param['date_end'])+3600*24-1 ] ];
            $vioWhere['a.create_time']=[['egt',strtotime($param['date_start']) ],['elt',strtotime($param['date_end'])+3600*24-1 ] ];
        }
        if (
            isset($param['sensitive_words_cate_id'])
            &&\think\Validate::is($param['sensitive_words_cate_id'],'number')
        ) {
            $where['filing_type']= $param['sensitive_words_cate_id'];
            $vioWhere['w.filing_type']= $param['sensitive_words_cate_id'];
        }
        $where['user_id'] = $param['user_id'];
        /**
         * 网站总数 	website_count
         * 正常访问 	request_success_count
         * 无法访问 	request_failed_count
         * 法人备案		group_count
         * 个人备案		personal_count
         * 境内接入 	within_border_count
         * 境外接入 	oversea_count
         */
        // 媒体存在构成图 [网站, 公众号, 微博, 论坛, 贴吧, APP]+
        $webModel = new Website();
        $site_count = $webModel->where($where)->count();
        // $site_data['website_count'] = $site_count;
        // dump($site_count);exit;
        $media_data = [
            'id' => 'media',
            'title' => '属地网络情况',
            'data' => array(
                ['name' => '网站['.$site_count.']', 'value' => $site_count],
                ['name' => '公众号[0]', 'value' => 0],
                ['name' => '微博[0]', 'value' => 0],
                ['name' => '论坛[0]', 'value' => 0],
                ['name' => '贴吧[0]', 'value' => 0],
                ['name' => 'APP[0]', 'value' => 0],
            ),
        ];

        $media_pie = $this->get_pie($media_data);

        

        // 属地网站概况 [正常访问, 无法访问, 法人备案, 个人备案, 境内接入, 境外接入]
        $site_detail_count = ['正常访问', '无法访问', '法人备案', '个人备案', '境内接入', '境外接入'];
        $site_list = $webModel->where($where)->select();

        $request_success_count = 0;
        $request_failed_count = 0;
        $group_count = 0;
        $personal_count = 0;
        $within_border_count = 0;
        $oversea_count = 0;

        foreach($site_list as $key => $value)
        {
            if($site_list[$key]['is_access'] == 1){
                $request_success_count += 1;
            } else if($site_list[$key]['is_access'] == 0) {
                $request_failed_count +=  1;
            }

            if($site_list[$key]['is_abroad'] == 1){
                $within_border_count += 1;
            } else if($site_list[$key]['is_abroad'] == 2) {
                $oversea_count += 1;
            }

            if($site_list[$key]['filing_type'] == 3){
                $group_count += 1;
            } else if($site_list[$key]['filing_type'] == 4) {
                $personal_count += 1;
            }
        }

        $site_list_count = [
            0 => $request_success_count, 
            1 => $request_failed_count, 
            2 => $within_border_count, 
            3 => $oversea_count,
            4 => $group_count,
            5 => $personal_count
        ];
        
        $site_data['site_list_count'] = $site_list_count;

        $with_site_data = [
            'id' => 'with_site',
            'title' => '属地网络情况',
            'category' => $site_detail_count,
            'data' => $site_list_count,
        ];

        $with_site_res = $this->get_bar($with_site_data);
        

        // 境内接入分布图 []
        $website_group_list = $webModel
            ->field('province, count(*) as count')
            ->where(['is_abroad'=> 1])
            ->where($where)
            ->group('province')
            ->select();

        $website_title_list = [];
        $website_data_list = [];
        $abroad_data = [];
        foreach($website_group_list as $key => $value) {
            $website_title_list[$key] = $value['province'].'['.$value['count'].']';
            $website_data_list[$key] = $value['count'];
            $abroad_data[$key] = ['name' => $value['province'].'['.$value['count'].']', 'value' => $value['count']];
        }

        $abroad_data = [
            'id' => 'abroad',
            'title' => '境内接入分布图',
            'data' => $abroad_data
        ];

        $abroad_res = $this->get_pie($abroad_data);

        $vio = db()
            ->name('vio_content_sensitive_words')
            ->alias("a")
            ->join('sensitive_words_category b', 'a.sensitive_words_category_id = b.id','left')
            ->join('vio_content vc', 'a.vio_content_id = vc.id','left')
            ->join('website w', 'vc.website_id = w.id','left')
            ->field('b.cate_name, count(DISTINCT a.website_id) as count')
            ->group('a.sensitive_words_category_id')
            ->order('count desc')
            ->where($vioWhere)
            ->where(['w.user_id'=>$param['user_id']])
            ->limit(4)
            // ->fetchSql(true)
            ->select();
        // var_dump($vio);exit;
        $access_category_data_list = [];

        foreach ($vio as $value) {
            $access_category_data_list[] = ['name'=> $value['cate_name'].'['.$value['count'].']','value' => $value['count']];
        }
        $access_category_data = [
            'id' => 'access_category',
            'title' => '可访问网站分类',
            'data' => $access_category_data_list
        ];

        $access_category_res = $this->get_pie($access_category_data);


        $this->view->engine->layout('layout/' . 'default');
        $this->assign('chart_with_site', $with_site_res);
        $this->assign('chart_media_pie', $media_pie);
        $this->assign('chart_abroad', $abroad_res);
        $this->assign('chart_access_category', $access_category_res);

        return $this->view->fetch();
    }

    public function get_bar($data)
	{
		$chart1 = new ECharts();
		$chart1->setJsVar($data['id']);
		$chart1->tooltip->show = true;
		$chart1->animation = false;
        $chart1->title[] = array(
            'show' => true, 
            'text' => $data['title'],
            'x' => 'center'
        );
		$chart1->legend[] = array(
			'orient' => 'vertical',
			'right' => 'right'
		);
		$chart1->xAxis[] = array(
			'type' => 'category',
            'minInterval' => 1,
			'data' => $data['category']
		);
		$chart1->yAxis[] = array(
            'minInterval' => 1,
			'type' => 'value'
		);
		$chart1->series[] = array(
			'type' => 'bar',
			'data' => $data['data']
		);

        $r = $chart1->render($data['id'], array('style' => 'width: 500px; height:350px;'));
        return $r;
	}

    public function get_pie($data){
        $chart2 = new ECharts();
        $chart2->setJsVar($data['id']);
        $chart2->animation = false;
        $chart2->title[] = array(
            'show' => true, 
            'text' => $data['title'],
            'x' => 'center'
        );
		$chart2->tooltip->show = true;
		$chart2->legend[] = array(
			'orient' => 'vertical',
			'right' => 'right'
		);
		$chart2->series[] = array(
			'type' => 'pie',
			'radius' => '50%',
            // ['value' => 1048, 'name' => 'Search Engine' ],
			'data' => $data['data']
		);

		$r = $chart2->render($data['id'], array('style' => 'width: 500px; height:350px;'));
        return $r;
		
    }

}
